/*
设置界面，大厅中的设置界面和游戏中的设置界面可能会有需要特殊处理的地方
*/
var gameData = require("gameData");
var confige = require("confige");
cc.Class({
    extends: cc.Component,

    properties: {
        parent:{
            default:null,
            type:cc.Node
        },
        sprArray :{
            default:[],
            type:[cc.SpriteFrame],
        },
        isInit:false,
    },

    onLoad: function () {
        this.musicBar = this.node.getChildByName("musicSlider").getChildByName("sliderBar").getComponent("cc.Sprite");
        this.soundBar = this.node.getChildByName("soundSlider").getChildByName("sliderBar").getComponent("cc.Sprite");
        this.node.getChildByName("musicSlider").getComponent("cc.Slider").progress = localSetting.gameData.musicValue;
        this.node.getChildByName("soundSlider").getComponent("cc.Slider").progress = localSetting.gameData.soundValue;
        this.soundBar.fillRange = localSetting.gameData.soundValue;
        this.musicBar.fillRange = localSetting.gameData.musicValue;
        this.chengeBgToggleGroup = this.node.getChildByName("chengeBgToggleGroup")
        this.ToggleGroup = {}
        for(var i=1;i<=6;i++)
        {
            var str = "toggle" + i
            this.ToggleGroup[i] = this.chengeBgToggleGroup.getChildByName(str).getComponent("cc.Toggle")
            if(i == localSetting.gameData.gameBgType )
            {
                this.ToggleGroup[i].isChecked = true
            }
            else
            {
                this.ToggleGroup[i].isChecked = false
            }
        }

    },

    onInit:function(){
        this.musicBar = this.node.getChildByName("musicSlider").getChildByName("sliderBar").getComponent("cc.Sprite");
        this.soundBar = this.node.getChildByName("soundSlider").getChildByName("sliderBar").getComponent("cc.Sprite");

        this.isInit = true;
    },

    sliderMove:function(event, customEventData){
        var index = parseInt(customEventData);
        if(index == 1)
        {
            this.soundBar.fillRange = event.progress;
            if(this.soundBar.fillRange >=0 && this.soundBar.fillRange <= 1)
                window.localSetting.gameData.soundValue = event.progress;
            if(this.soundBar.fillRange == 0)
                window.localSetting.gameData.soundValue = 0.000001;
            // console.log(window.localSetting.gameData.soundValue);
            if(event.progress > 0)
                window.localSetting.gameData.soundEnable = true;
            else
                window.localSetting.gameData.soundEnable = false;
        }
        if(index == 2)
        {
            this.musicBar.fillRange = event.progress;
            if(this.musicBar.fillRange >=0 && this.musicBar.fillRange <= 1)
                window.localSetting.gameData.musicValue = event.progress;
            if(this.musicBar.fillRange == 0)
                window.localSetting.gameData.musicValue = 0.000001;
            // console.log(window.localSetting.gameData.musicValue);
            if(event.progress > 0)
                window.localSetting.gameData.musicEnable = true;
            else
                window.localSetting.gameData.musicEnable = false;

            if(this.musicBar.fillRange >=0 && this.musicBar.fillRange <= 1)
                cc.audioEngine.setVolume(window.DKTools.audioEG.audioBgId,window.localSetting.gameData.musicValue);
        }
        window.localSetting.saveGameData();
    },

    btnExitClick:function(){
        // window.DKTools.exitGame();
        // this.parent.btnClickRefresh();
        cc.sys.localStorage.setItem("wxLastLoginDay", null);
        cc.sys.localStorage.setItem("wxRefreshToken", null);
        cc.sys.localStorage.setItem("userSetting", null);
        cc.sys.localStorage.setItem("firstOpen", -1);
        window.DKTools.audioEG.stopBg();
        this.scheduleOnce(function(){
            cc.game.restart();
        },0.1);
    },

    showLayer:function(isHall){
        if(this.isInit == false)
            this.onInit();
        this.node.active = true;
        if(isHall || confige.roomData.roomType == "PaiGowBalcony")
        {
            this.chengeBgToggleGroup.active = false
            this.node.getChildByName("chengeBgLabel").active = false
        }
        console.log(confige.roomData.gameMode)
    },
    selectChangeGamebg:function(event, customEventData){
        var index = parseInt(customEventData);
        window.localSetting.gameData.gameBgType = index
        var chengeBgToggleGroup = this.node.getChildByName("chengeBgToggleGroup")
        console.log(this.ToggleGroup)
        for(var i=1;i<=6;i++)
        {
            this.ToggleGroup[i].isChecked = false;
        }
        this.ToggleGroup[index].isChecked = true;
        gameData.gameBGNode.node.getChildByName("mainBg").getComponent("cc.Sprite").spriteFrame = this.sprArray[index-1]       
        window.localSetting.saveGameData();
    },

    hideLayer:function(){
        this.node.active = false;
    },
});
